package com.daotingli.backendprograms.Repository.Author.individual;

import com.daotingli.backendprograms.Bean.Bean.t_userBean;
import com.daotingli.backendprograms.DB.SpringDB;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Repository
public class IndividualRepository {
    @Autowired
    private t_userBean t_user;
    @Autowired
    private SpringDB springDB;
    public List<t_userBean> getAll(String username, String password) {
        String sql="select * from t_user where username=? and password=?";
        List<t_userBean> list =new ArrayList<>();
        try {
            springDB.getJdbcTemplate().query(sql, (rs, rowNum) -> {
                t_user = new t_userBean(rs.getInt("id"), rs.getString("username"), rs.getString("password"), rs.getInt("identity"), rs.getString("userphone"), rs.getInt("userstate"));
                list.add(t_user);
                return t_user;
            },username,password);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return list;
    }

    public Map<String, Object> edit(int id, String username, String password, String identity, String userphone) {
        String sql = "update t_user set username=?,password=?,identity=?,userphone=? where id=?";
        return springDB.getJdbcTemplate().update(sql, username, password, identity, userphone, id) > 0 ?
                Map.of("message", "修改成功") :
                Map.of("message", "修改失败");

    }

    public List<Integer> getAuthorBookIDs(int id) {
        String sql = "select id from book where author_id=?";
        List<Integer> list = new ArrayList<>();
        try {
            springDB.getJdbcTemplate().query(sql, (rs, rowNum) -> {
                list.add(rs.getInt("id"));
                return rs.getInt("id");
            }, id);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return list;
    }
}
